<?php

namespace Home\Model;

use Think\Model;

class CommentModel extends Model {

	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = '') {
		$page = $page < 1 ? 1 : $page;
		$num = $num ? $num : 30;

		// 读取缓存
		$cacheName = get_cache_name ( 'search_comment_' . $where . $page . $order . $num );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}

		$result ['list'] = $this->field ( 'id' )->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			$result ['list'] [$k] = $this->getInfo ( $v ['id'] );
		}
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		S ( $cacheName, $result, C ( 'cache_time_short' ) );
		return $result;
	}
	/**
	 * 获取单条信息
	 *
	 * @param unknown $id
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, mixed, unknown, string, object>
	 */
	public function getInfo($id) {
		if (intval ( $id )) {
			$where = 'id=' . intval ( $id );
		} else {
			$where = 'url="' . $id . '"';
		}

		// 读取缓存
		$cacheName = get_cache_name ( 'comment_' . $id );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}

		$user = new UserModel ();

		$result = $this->where ( $where )->find ();
		$result ['text200'] = cut_str ( strip_tags ( $result ['context'] ), 200, '' );
		$userInfo = $user->getUserInfo ( $result ['uid'] );
		$result ['username'] = $userInfo ['username'] ? $userInfo ['username'] : '枫桥居花友';
		$result ['avatar'] = $userInfo['avatar'];
		if ($result ['obj_type'] == 'site') {
			$site = new SiteModel ();
			$result ['obj_info'] = $site->getInfo ( $result ['obj_id'] );
		} elseif ($result ['obj_type'] == 'article') {
			$article = new ArticleModel ();
			$result ['obj_info'] = $article->getInfo ( $result ['obj_id'] );
		} elseif ($result ['obj_type'] == 'blog') {
			$blog = new BlogModel ();
			$result ['obj_info'] = $blog->getInfo ( $result ['obj_id'] );
		}

		S ( $cacheName, $result, C ( 'cache_time_long' ) );
		return $result;
	}
	/**
	 * 保存数据
	 *
	 * @param unknown $data
	 * @return string
	 */
	public function saveData($data) {
		$data ['uid'] = get_cookie ( 'uid' );
		if (! $data ['obj_type']) {
			$result ['status'] = 0;
			$result ['msg'] = '对象类型不能空';
		} elseif (! $data ['obj_id']) {
			$result ['status'] = 0;
			$result ['msg'] = '对象ID不能空';
		} elseif (! $data ['context']) {
			$result ['status'] = 0;
			$result ['msg'] = '评论内容不能空';
		} elseif ( strlen($data ['context'])>500) {
			$result ['status'] = 0;
			$result ['msg'] = '评论内容长度不能大于500个字符,现在是:'.strlen($data ['context']);
		} else {
			$data ['createtime'] = date ( 'Y-m-d H:i:s' );
			$data ['createip'] = get_user_ip ();
			$data ['from_url'] = $_SERVER ['HTTP_REFERER'];
			$rs = $this->add ( $data );
			if ($rs) {
				if (! $data ['uid']) {
					$msg = '您也可以<a href="javascript:" onclick="$(\'.login-btn\').click()">登录</a>枫桥居账号以便日后查询!';
				}
				$cacheName = get_cache_name ( 'search_comment_obj_id='.$data['obj_id'].' and obj_type="'.$data['obj_type'].'"1id desc30' );
				S ( $cacheName, NULL );
				$result ['status'] = 1;
				$result ['msg'] = '添加成功!' . $msg;
				$result ['id'] = $rs;
			} else {
				$result ['status'] = 0;
				$result ['msg'] = '添加失败';
			}
		}
		return $result;
	}

	/**
	 * 获内容取评论列表
	 *
	 * @param unknown $obj_id
	 * @param unknown $obj_type
	 * @return Ambigous <\Home\Model\unknown, number, string>
	 */
	public function getComment($obj_id, $obj_type) {
		$where = 'obj_id=' . $obj_id . ' and obj_type="' . $obj_type . '"';
		$page = intval ( $_REQUEST ['page'] );
		$order = 'id desc';
		$result = $this->search ( $where, $page, $order );
		return $result;
	}

	/**
	 * 获取登录用户的评论列表
	 */
	public function getUserComment() {
		$uid = get_cookie ( 'uid' );
		$where = 'uid=' . intval ( $uid );
		$page = intval ( $_REQUEST ['page'] );
		$order = 'id desc';
		$result = $this->search ( $where, $page, $order );
		return $result;
	}
}
